import {
  CHANGE_TOP_BANNERS,
  CHANGE_HOT_RECOMMEND,
  CHANGE_NEW_ALBUM,
  CHANGE_LIST_0,
  CHANGE_LIST_2,
  CHANGE_LIST_3,
} from "./constant";
import {
  getTopBanners,
  getHotRecmmends,
  getNewAlbums,
  getList,
} from "network/recommend";

export const changeBannerAction = (topBanners) => ({
  type: CHANGE_TOP_BANNERS,
  topBanners,
});

export const changeRecmmendAction = (hotRecommend) => ({
  type: CHANGE_HOT_RECOMMEND,
  hotRecommend,
});

export const changeNewAlbum = (newAlbum) => ({
  type: CHANGE_NEW_ALBUM,
  newAlbum,
});
export const changeTopList_0 = (toplist) => ({
  type: CHANGE_LIST_0,
  toplist,
});
export const changeTopList_2 = (newList) => ({
  type: CHANGE_LIST_2,
  newList,
});
export const changeTopList_3 = (originlist) => ({
  type: CHANGE_LIST_3,
  originlist,
});

export const getTopBannerAction = () => {
  return (dispatch) => {
    getTopBanners().then((res) => {
      dispatch(changeBannerAction(res.banners));
    });
  };
};

export const getHotRecAction = (limit) => {
  return (dispatch) => {
    getHotRecmmends(limit).then((res) => {
      dispatch(changeRecmmendAction(res.result));
    });
  };
};

export const getNewAluAction = (limit, offset) => (dispatch) => {
  getNewAlbums(limit, offset).then((res) => {
    dispatch(changeNewAlbum(res.albums));
  });
};

export const getListAction = (idx) => (dispatch) => {
  getList(idx).then((res) => {
    switch (idx) {
      case 0:
        dispatch(changeTopList_0(res));
        break;
      case 2:
        dispatch(changeTopList_2(res));
        break;
      case 3:
        dispatch(changeTopList_3(res));
        break;
      default:
        return idx;
    }
  });
};
